package com.rraj.common.mq;

import java.util.Properties;

import javax.annotation.Resource;

import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.order.OrderProducer;
import com.aliyun.openservices.ons.api.transaction.TransactionProducer;
import com.rraj.common.mq.mqenum.MQProducerEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * Created by WangSheng on 2017/6/8.
 *
 * @author WangSheng
 */
@Configuration
@Lazy
public class MQProducerConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(MQProducerConfig.class);
    @Value("${aliyunMQ.access_key}")
    private String accessKey;
    @Value("${aliyunMQ.secret_key}")
    private String secretKey;

    @Resource
    private MQLocalTransactionCheckerImpl localTransactionCheckerImpl;

    /**
     * 普通消息生产者
     *
     * @return 普通消息生产者
     */
    @Bean
    public Producer defaultProducer() {
        Producer producer = ONSFactory.createProducer(
            this.getProducerProperties(MQProducerEnum.PID_DEFAULT.getPid()));
        producer.start();
        LOGGER.info("普通消息生产者启动成功！PID为：" + MQProducerEnum.PID_DEFAULT.getPid());
        return producer;
    }

    /**
     * 全局顺序消息生产者
     *
     * @return 全局顺序消息生产者
     */
    @Bean
    public OrderProducer orderAllProducer() {
        OrderProducer producer = ONSFactory.createOrderProducer(
            this.getProducerProperties(MQProducerEnum.PID_ALL_ORDER.getPid()));
        producer.start();
        LOGGER.info("全局顺序消息生产者启动成功！PID为：" + MQProducerEnum.PID_ALL_ORDER.getPid());
        return producer;
    }

    /**
     * 分区顺序消息生产者
     *
     * @return 分区顺序消息生产者
     */
    @Bean
    public OrderProducer orderShardingProducer() {
        OrderProducer producer = ONSFactory.createOrderProducer(
            this.getProducerProperties(MQProducerEnum.PID_SHARDING_ORDER.getPid()));
        producer.start();
        LOGGER.info("分区顺序消息生产者启动成功！PID为：" + MQProducerEnum.PID_SHARDING_ORDER.getPid());
        return producer;
    }

    /**
     * 事务消息生产者
     *
     * @return 事务消息生产者
     */
    @Bean
    public TransactionProducer transactionProducer() {
        TransactionProducer producer = ONSFactory.createTransactionProducer(
            this.getProducerProperties(MQProducerEnum.PID_TRANSACTION.getPid()),
            localTransactionCheckerImpl);
        producer.start();
        LOGGER.info("事务消息生产者启动成功！PID为：" + MQProducerEnum.PID_TRANSACTION.getPid());
        return producer;
    }

    private Properties getProducerProperties(String producerID) {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.ProducerId, producerID);
        properties.put(PropertyKeyConst.AccessKey, accessKey);
        properties.put(PropertyKeyConst.SecretKey, secretKey);
        return properties;
    }

}
